import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '*',
    name: 'shouye',
    meta: { title: '首页' },
    component: () => import('../views/home.vue'),
    // redirect: '/shouye:list'
  },
  {
    path: '/',
    name: 'shouye',
    meta: { title: '首页' },
    component: () => import('../views/home.vue'),
    // redirect: '/shouye:list'
  },

  {
    path: '/shouye:list',
    name: 'shouye',
    meta: { title: '首页' },
    component: () => import('../views/home.vue'),

  },
  {
    path: '/cat',
    name: 'cat',
    meta: { title: '购物车' },
    component: () => import('../views/cat.vue')
  },
  //分类
  {
    path: '/branch',
    name: 'branch',
    meta: { title: '分类' },
    component: () => import('../views/branch.vue'),

    children: [
      {
        path: '',
        name: '',
        component: () => import('../views/fenlei.vue')
      },
      {
        path: 'fenlei/:i',
        name: 'fenlei',
        component: () => import('../views/fenlei.vue'),
      },

    ]
  },
  {
    path: '/my',
    name: 'my',
    meta: { title: '我的' },
    component: () => import('../views/my.vue')
  },
  {
    path: '/login',
    name: 'login',
    meta: { title: '登录' },
    component: () => import('../views/login2.vue')
  },
  //详情页
  {
    path: '/xiangqing',
    name: 'xiangqing',
    meta: { title: '商品详情' },
    component: () => import('../views/xiangQing.vue')
  },
  //搜索
  {
    path: '/sousuo',
    name: 'sousuo',
    meta: { title: '搜索' },
    component: () => import('../views/sousuo.vue')
  },
  //列表
  {
    path: '/list',
    name: 'list',
    meta: { title: '商品列表' },
    component: () => import('../views/list.vue')
  },
  //分类结果
  {
    path: 'fenleijieguo',
    name: 'fenleijieguo',
    component: () => import('../views/fenleijieguo.vue'),
  },
  //收货地址
  {
    path: 'shouhuodizhi',
    name: 'shouhuodizhi',
    component: () => import('../views/shouhuodizhi.vue'),
  },
  //新增收货地址
  {
    path: 'addshouhuodizhi',
    name: 'addshouhuodizhi',
    component: () => import('../views/addshouhuodizhi.vue'),
  },
  //新增收货地址
  {
    path: 'dingdantianxie',
    name: 'dingdantianxie',
    component: () => import('../views/dingdantianxie.vue'),
  },
  // 订单
  {
    path: 'dingdan',
    name: 'dingdan',
    component: () => import('../views/dingdan.vue'),
    children: [
      {
        path: 'dingdanlist',
        name: 'dingdanlist',
        component: () => import('../views/dingdanlist.vue'),
      }
    ]
  },

]

const router = new VueRouter({
  routes
})
//全局前置守卫：初始化时执行、每次路由切换前执行
router.beforeEach((to, from, next) => {
  let from1 = {
    name: from.name,
    query: from.query,
    params: from.params
  }
  console.log(to);
  if (to.meta.title) {
    document.title = to.meta.title
  }
  sessionStorage.setItem('shang', JSON.stringify(from1))
  if (to.name == 'my' || to.name == 'cat') {
    if (sessionStorage.getItem('token')) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router
